package org.jeecgframework.web.service.impl;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.pojo.base.CbsContractEntity;
import org.jeecgframework.web.system.pojo.base.CbsContractMcEquipmentEntity;
import org.jeecgframework.web.service.CbsContractMcEquipmentServiceI;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.util.StaticValue;
import org.joda.time.DateTime;
import org.joda.time.Months;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.io.Serializable;

@Service("cbsContractMcEquipmentService")
//@Transactional
public class CbsContractMcEquipmentServiceImpl extends CommonServiceImpl implements CbsContractMcEquipmentServiceI {
	private static final Logger logger = Logger.getLogger(CbsContractMcEquipmentServiceImpl.class);


	//定时检查合同失效
	public void loseContract() {
		System.out.println("定时任务开始：检查合同是否失效...");
		//获取系统当前时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		java.util.Date date = c.getTime();
		String s = sdf.format(date);
		System.out.println(s);
		CbsContractMcEquipmentEntity cbsContractMcEquipmentEntity = new CbsContractMcEquipmentEntity();
		List<CbsContractMcEquipmentEntity> list = findHql("from CbsContractMcEquipmentEntity where status='"+ StaticValue.mc_status_2+"' and serviceEndDate<'"+s+"'");
		if(list.size() != 0){
			for(int i = 0;i<list.size();i++){
				logger.info("查询集合:"+list.get(i));
				cbsContractMcEquipmentEntity.setId(list.get(i).getId());
				cbsContractMcEquipmentEntity.setStatus(4);
				CbsContractMcEquipmentEntity t = get ( CbsContractMcEquipmentEntity.class, cbsContractMcEquipmentEntity.getId () );
				try {
					MyBeanUtils.copyBeanNotNull2Bean ( cbsContractMcEquipmentEntity, t );
					saveOrUpdate ( t );
				} catch (Exception e) {
					e.printStackTrace ();
					throw new BusinessException( e.getMessage () );
				}
			}
		}
	}

	//检查合同生效
	public void effectContract() {
		System.out.println("定时任务开始：检查合同是否生效...");
		//获取系统当前时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		java.util.Date date = c.getTime();
		String time = sdf.format(date);
		System.out.println(time);
		CbsContractMcEquipmentEntity cbsContractMcEquipmentEntity = new CbsContractMcEquipmentEntity();
		String sql = "select me.*,cc.inspection_provider from cbs_contract_mc_equipment me left join Cbs_Contract cc on cc.contract_code = me.contract_code where me.status = '1' AND cc.pay_Status = '2' AND me.service_Start_Date <='"+time+"'";
		List<Map<String, Object>> list = findForJdbc(sql);
		if(list.size() != 0){
			for(int i = 0;i<list.size();i++){
				logger.info("查询集合:"+list.get(i));
				List<TSDepart> byProperty = findByProperty(TSDepart.class, "orgCode", list.get(i).get("inspection_provider"));
				Date startDate = (Date) list.get(i).get("service_Start_Date");
				Date endDate = (Date) list.get(i).get("service_End_Date");
				String sn = (String) list.get(i).get("sn");
				updateBySqlString("update zy_equipment_spare set send_sign='"+byProperty.get(0).getId()+"' where sn='"+sn+"'");
				updateBySqlString("update zy_equipment_spare set warranty_begin_date='"+startDate+"',repair_end_date='"+endDate+"' where sn='"+sn+"'");
				if(StringUtil.isNotEmpty(startDate)&&StringUtil.isNotEmpty(endDate)){
					String s = startDate.toString().substring(0, 10);
					String s1 = endDate.toString().substring(0, 10);
					DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
					DateTime start = formatter.parseDateTime(s);
					DateTime end = formatter.parseDateTime(s1);
					int months = Months.monthsBetween(start, end).getMonths();
					if(months>=6){
						updateBySqlString("update zy_equipment_spare set residual_ip_count=residual_ip_count+1 where sn='"+sn+"'");
					}
				}
				Map<String, Object> map = list.get(i);
				String id = (String) map.get("id");
				cbsContractMcEquipmentEntity.setId(id);
				cbsContractMcEquipmentEntity.setStatus(2);
				CbsContractMcEquipmentEntity t = get ( CbsContractMcEquipmentEntity.class, cbsContractMcEquipmentEntity.getId () );
				try {
					MyBeanUtils.copyBeanNotNull2Bean ( cbsContractMcEquipmentEntity, t );
					saveOrUpdate ( t );
				} catch (Exception e) {
					e.printStackTrace ();
					throw new BusinessException( e.getMessage () );
				}
			}
		}
	}
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((CbsContractMcEquipmentEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
 		this.doAddSql((CbsContractMcEquipmentEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
 		this.doUpdateSql((CbsContractMcEquipmentEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(CbsContractMcEquipmentEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(CbsContractMcEquipmentEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(CbsContractMcEquipmentEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,CbsContractMcEquipmentEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{contract_id}",String.valueOf(t.getContractId()));
 		sql  = sql.replace("#{sn}",String.valueOf(t.getSn()));
 		sql  = sql.replace("#{equipment_type}",String.valueOf(t.getEquipmentType()));
 		sql  = sql.replace("#{service_start_date}",String.valueOf(t.getServiceStartDate()));
 		sql  = sql.replace("#{service_end_date}",String.valueOf(t.getServiceEndDate()));
 		sql  = sql.replace("#{mc_code}",String.valueOf(t.getMcCode()));
 		sql  = sql.replace("#{status}",String.valueOf(t.getStatus()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
}